import os
import shutil

def move_matching_images(folder_a, folder_b, folder_c):
    # 确保目标文件夹 C 存在
    os.makedirs(folder_c, exist_ok=True)

    # 获取 A 文件夹中的所有文件名（不含扩展名）
    image_names_a = {os.path.splitext(f)[0] for f in os.listdir(folder_a) if f.lower().endswith(('png', 'jpg', 'jpeg', 'bmp', 'tif', 'tiff'))}

    # 遍历 B 文件夹，查找匹配的文件
    for file_name in os.listdir(folder_b):
        name, ext = os.path.splitext(file_name)
        if name in image_names_a and ext.lower() in ['.png', '.jpg', '.jpeg', '.bmp', '.tif', '.tiff']:
            source_path = os.path.join(folder_b, file_name)
            destination_path = os.path.join(folder_c, file_name)
            shutil.move(source_path, destination_path)
            print(f"Moved: {file_name} -> {folder_c}")

if __name__ == "__main__":
    folder_a = "../background/edge"  # A 文件夹路径
    folder_b = "output_slices"  # B 文件夹路径
    folder_c = "../background2/edge"  # C 文件夹路径

    move_matching_images(folder_a, folder_b, folder_c)
